package cn.smartorigin.soas.frame;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import cn.smartorigin.soas.data.SqliteConnecter;
import cn.smartorigin.soas.data.TestData;
import cn.smartorigin.soas.frame.panel.ManageClientPage;

public class AddClient extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	JButton mCfnBtn, mClrBtn;
	JTextField mClientName;
	MainFrame mf = null;
	ManageClientPage mcp = null;

	public AddClient(ManageClientPage clp, MainFrame mfc){
		//mf = mainframe;
		this.mcp = clp;
		this.mf = mfc;
		this.setTitle(" 添加客户  - SOAS");
		this.setIconImage(new ImageIcon("img/sosa_logo.png").getImage());
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setSize(600,400);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		initBackGround();
		initFont();
		compentInit();
		this.setVisible(true);
	}
	
	private void initFont(){
		Font font = new Font("宋体", Font.PLAIN, 12);
		UIManager.put("Button.font", font);
		UIManager.put("Label.font", font);

	}
	
	private void initBackGround(){	  
		  ImagePanel ip = new ImagePanel(new ImageIcon("res/images/dialog_bg.png"));
		  this.setContentPane(ip); 
	}
		
	private void compentInit(){
		
		BtnListener btnlistener = new BtnListener();
		
		ImageIcon clrBtnIcon=new ImageIcon("img/ic_delete.png"); 
		ImageIcon clrCfnIcon=new ImageIcon("img/corfim.png");
		mCfnBtn = new JButton("确定", clrCfnIcon);
		mCfnBtn.setBounds(250,300,100,30);
		mCfnBtn.addActionListener(btnlistener);
		mClrBtn = new JButton("取消",clrBtnIcon);
		mClrBtn.setBounds(450,300,100,30);
		mClrBtn.addActionListener(btnlistener);
		
		JLabel clientName  = new JLabel("客户名称：");
		clientName.setFont(new Font(clientName.getFont().getName(),Font.BOLD,clientName.getFont().getSize()));
		clientName.setBounds(250,100,100,30);
		mClientName = new JTextField();
		mClientName.setBounds(350,100,200,30);

		
		this.setLayout(null);
		this.add(clientName);
//		this.add(clientDesc);
		this.add(mClientName);
//		this.add(mClientDesc);
		this.add(mCfnBtn);
		this.add(mClrBtn);
	}
	
	private  void frameClose(){
		mcp.flashClientTable();
		mf.reflashData();
		this.dispose();
	}
	
	private void saveData(){
		int err;
		String str1 = mClientName.getText();
		if((str1!=null))
			try {
				err = TestData.insertClinet(new SqliteConnecter(), str1);
				if(err !=0){
					JOptionPane.showMessageDialog(null, "客户名已经存在，不能重复 ！", "警告", JOptionPane.WARNING_MESSAGE);
				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	class BtnListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			Object  obj =(Object)e.getSource();
			if(obj == mCfnBtn){
				saveData();
				//mf.reflashData();
				frameClose();
			}else if(obj == mClrBtn){
				frameClose();
			}
		}
	}
	
}
